package com.jsonpfy.operations;

import java.util.Date;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;

/** 
 * Send a particular registry to the datastore.
 * 
 * @
 * @Feature 
 */

public class Put {

	public static void doPut(String kind, String ID, String[] fieldsKind,
			String[] fieldsName, String[] fieldsValue) {

		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Entity entity = null;

		if (ID != null) {

			entity = Get.getEntity(kind, ID);

			if (entity == null) {

				entity = new Entity(kind, ID);
			}

		} else {

			entity = new Entity(kind);
		}

		for (int i = 0; i < fieldsName.length; i++) {

			if ("String".equals(fieldsKind[i])) {

				entity.setProperty(fieldsName[i], fieldsValue[i]);

			} else if ("Date".equals(fieldsKind[i])) {

				entity.setProperty(fieldsName[i], new Date(new Long(
						fieldsValue[i])));

			} else {

				entity.setProperty(fieldsName[i],
						KeyFactory.createKey(fieldsKind[i], fieldsValue[i]));
			}

		}

		datastore.put(entity);
	}
}